/// Setting up the ANES File for presidential coalitions  

use "/Users/joshuazingher/Desktop/Prospectus /anes_cdfdta/anes coalitions .dta"

sort year

rename VCF0704 presvote

rename VCF0703 voted


/// Generating total religion respondants by year

by year: egen Religionresp = count(religion) if religion == 2

/// Generating total number of religion voters by year


by year: egen religionvoted = count(voted) if religion==2 & voted==3

/// Generating total number of Latnio non-voters by year

by year: egen religionNvoted = count(voted) if voted==0 & religion==2 | voted==1 & religion==2 | voted==2 & religion==2

/// Generating total number of non-religion voters by year

by year: egen Nreligionvoted = count(voted) if religion~=2 & voted==3

/// Generating total number of non-religion non-voters by year

by year: egen NreligionNvoted = count(voted) if religion~=2 & voted==0 | religion~=2 & voted==1 | religion~=2 & voted==2

/// filling in the blanks

by year: egen Religionvoted = max(religionvoted)
by year: egen ReligionNvoted = max(religionNvoted)
by year: egen NReligionvoted = max(Nreligionvoted)
by year: egen NReligionNvoted = max(NreligionNvoted)




/// Generating % religion TO with Mosteller Correction

gen N = 1
gen a = [Religionvoted * NReligionNvoted] / [ReligionNvoted * NReligionvoted]


///Generating filler vars

gen aminone =(a-1)
gen AreligionplusTO = (ACatholic+aTO)
gen areligionTO=(a*ACatholic*aTO)

gen CreligionTO = 1/ (2*(aminone)) * (N+(aminone*AreligionplusTO) - sqrt((N+aminone*AreligionplusTO)*(N+aminone*AreligionplusTO) - (4*(aminone*areligionTO))))

///Adjusting TO for the formula

replace CreligionTO = CreligionTO/ACatholic



/// Moving to Partisanship - Generating religion Democratic Vote total by year

by year: egen religiondem = count(religion) if presvote==1 & religion==2

/// Generating religion Non-Democratic Vote by Year

by year: egen religionNdem = count(religion) if presvote==2 & religion==2

// Generating non-religion Democratic Vote total by year

by year: egen Nreligiondem = count(religion) if presvote==1 & religion~=2

/// Generating non-religion non-Democratic vote total by year

by year: egen NreligionNdem = count(religion) if presvote==2 & religion~=2

/// filling in the blanks

by year: egen ReligionDem = max(religiondem)
by year: egen ReligionNDem = max(religionNdem)
by year: egen NReligionDem = max(Nreligiondem)
by year: egen NReligionNDem = max(NreligionNdem)

/// Generation % religion Democrat with Mosteller Correction

gen A = (ReligionDem*NReligionNDem)/(ReligionNDem*NReligionDem)
gen Aminone =(A-1)
gen AreligionplusDem = (ACatholic+ADem)
gen AreligionDem=(A*ACatholic*ADem)
gen CreligionDem = 1/(2*(Aminone)) * (N+(Aminone*AreligionplusDem) - sqrt((N+Aminone*AreligionplusDem)*(N+Aminone*AreligionplusDem) - (4*(Aminone*AreligionDem))))

replace CreligionDem = CreligionDem/ACatholic


/// Generating number of ANES respondents by year

by year: egen totalresp = count (VCF0006a)


/// Coalition Contribution

gen Religioncoalcontrib = (CreligionTO* CreligionDem *ACatholic)/(aTO*ADem)



